package cn.base.web.cahe;

import com.github.benmanes.caffeine.cache.Expiry;
import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.io.Serializable;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;

/**
 * 每次访问后更新有效期
 * @param <K>
 * @param <V>
 */
public class SessionExpiry<K, V> implements Expiry<K, V>, Serializable {
    private static final long serialVersionUID = 1L;
    private final Duration expireTime;

    public SessionExpiry(Duration expireTime) {
        this.expireTime = expireTime;
    }
    @Override
    public long expireAfterCreate(@NonNull Object key, @NonNull Object value, long currentTime) {
        return expireTime.toNanos();
    }

    @Override
    public long expireAfterUpdate(@NonNull Object key, @NonNull Object value, long currentTime, @NonNegative long currentDuration) {
        //currentDuration是剩余时间
        return expireTime.toNanos();
    }

    @Override
    public long expireAfterRead(@NonNull Object key, @NonNull Object value, long currentTime, @NonNegative long currentDuration) {
        return expireTime.toNanos();
    }
}
